﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace nr.Tombola.Windows.Table
{
    public partial class MainForm : Form
    {
        private nr.Tombola.Net.GameClient client;

        public MainForm()
        {
            InitializeComponent();
        }

        private void txtServerAddress_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                System.Net.Dns.GetHostEntry(txtServerAddress.Text);
            }
            catch
            {
                txtServerAddress.SelectAll();
                e.Cancel = true;
            }
        }

        private void txtServerPort_Validating(object sender, CancelEventArgs e)
        {
            int value;
            if (int.TryParse(txtServerPort.Text, out value))
                e.Cancel = ((value < 1000) || (value > 9999));
            else
                e.Cancel = true;

            if (e.Cancel) txtServerPort.SelectAll();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            txtServerAddress.Text = "127.0.0.1";
            txtServerPort.Text = "9090";
        }

        private void miConnectionConnect_Click(object sender, EventArgs e)
        {
            client = new Net.GameClient(txtServerAddress.Text, int.Parse(txtServerPort.Text));
            client.NumberExtract += client_NumberExtract;
            client.StartGame += client_StartGame;
            client.EndGame += client_EndGame;
            client.GameStatus += client_GameStatus;
            try
            {
                client.Connect();
            }
            catch
            {
                MessageBox.Show(this, "Impossibile collegarsi al server.", "[nr.Tombola] - Tabellone", MessageBoxButtons.OK);
            }
        }

        void client_GameStatus(object sender, Net.GameStatusEventArgs e)
        {
            Clear();
            foreach (int number in e.Numbers)
                GetTombolaNumber(number).Extract = true;
        }

        void client_EndGame(object sender, Net.EndGameEventArgs e)
        {
            BeginInvoke(new MessageBoxDelegate(ShowMessage), "La partita è terminata!");
            Clear();
        }

        private delegate void MessageBoxDelegate(string message);

        private void ShowMessage(string message)
        {
            MessageBox.Show(this, message, "[nr.Tombola] Server di Gioco", MessageBoxButtons.OK);
        }

        private nr.Tombola.Controls.TombolaNumber GetTombolaNumber(int number)
        {
            string controlName = string.Format("tombolaNumber{0}", number);
            Control[] result = layout.Controls.Find(controlName, false);
            if ((result != null) && (result.Length > 0))
                return (nr.Tombola.Controls.TombolaNumber)result[0];
            else
                return null;
        }

        void client_StartGame(object sender, Net.StartGameEventArgs e)
        {
            Clear();
            BeginInvoke(new MessageBoxDelegate(ShowMessage), "Inizia una nuova partita!");
        }

        void client_NumberExtract(object sender, Net.NumberExtractEventArgs e)
        {
            nr.Tombola.Controls.TombolaNumber control = GetTombolaNumber(e.Number);
            if (control != null)
                control.Extract = true;
        }

        void Clear()
        {
            for (int index = 1; index < 91; ++index)
                GetTombolaNumber(index).Extract = false;
        }

        private void miGameRedraw_Click(object sender, EventArgs e)
        {
            Invalidate(true);
        }
    }
}
